<?php
#by reemo 2012
class ShowMarktupPage extends AbstractPage
{	
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	
	function kaufen()
	{
		global $USER, $resource, $reslist;
		
		$row_id    	= HTTP::_GP('id', 0);
		$row		= $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".MUP." WHERE id = '".$row_id."';");
		
		if(empty($row))
		{
			$this->printMessage('Апгрейд уже не найден', true, array("game.php?page=marktup", 1));
			return;
		}	
		
		if ($row['cena_tm'] > $USER['darkmatter'])
		{
			$this->printMessage('Недостаточно Тёмной материи', true, array("game.php?page=marktup", 2));
			return;
		}		
		if ($row['cena_am'] > $USER['atm'])
		{
			$this->printMessage('Недостаточно Антиматерии', true, array("game.php?page=marktup", 2));
			return;
		}	
				
		$USER['atm'] 		-= $row['cena_am'];
		$USER['darkmatter'] -= $row['cena_tm'];
		
		$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET
								darkmatter = darkmatter + ".$row['cena_tm'].", 
								atm = atm + ".$row['cena_am']."
								WHERE id = ".$row['id_owner'].";");
								
		$GLOBALS['DATABASE']->query("DELETE FROM ".MUP." WHERE `id` = ".$row['id']." LIMIT 1;");
		$GLOBALS['DATABASE']->query("UPDATE ".UP." SET ".$row['greid']."_".$row['ship']." = ".$row['greid']."_".$row['ship']." + ".$row['etap']." WHERE id_owner = ".$USER['id'].";");
		$this->printMessage('Ангрейд преобретен', true, array("game.php?page=marktup", 1));
	}
		
	function abholen()
	{
		global $USER, $resource, $reslist;
		
		$row_id    	= HTTP::_GP('id', 0);
		$row		= $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".MUP." WHERE id = '".$row_id."';");
		
		if ($row['id_owner'] != $USER['id'])
		{
			$this->redirectTo('game.php?page=marktup');
			return;
		}		
		
		$GLOBALS['DATABASE']->query("DELETE FROM ".MUP." WHERE `id` = ".$row_id." LIMIT 1;");		
		$GLOBALS['DATABASE']->query("UPDATE ".UP." SET ".$row['greid']."_".$row['ship']." = ".$row['greid']."_".$row['ship']." + ".$row['etap']." WHERE id_owner = ".$USER['id'].";");
		$this->redirectTo('game.php?page=marktup');
	}
	
		
	public function show()
	{
		global $ProdGrid, $LNG, $resource, $reslist, $CONF, $PLANET, $USER, $pricelist, $requeriments;
		
		$MupList    = array();
		$nomer		= 0;
		$Mup 		= $GLOBALS['DATABASE']->query("SELECT * FROM ".MUP." ORDER BY ship ASC;");
		
		while($row = $GLOBALS['DATABASE']->fetch_array($Mup))
		{
			$nomer		+= 1;
			
			switch($row['greid'])
			{
				case 'a':$nazvanie = "Орудия"; break;
				case 's':$nazvanie = "Щитов"; break;
				case 'd':$nazvanie = "Брони"; break;
			}
			
			if($row['id_owner'] == $USER['id'])
			{
				$silka = "<a style=\"color:#f00;\" href=\"game.php?page=marktup&amp;mode=abholen&amp;id=".$row['id']."\">Забрать</a>";
			}
			else
			{
				$silka = "<a href=\"game.php?page=marktup&amp;mode=kaufen&amp;id=".$row['id']."\">Купить</a>";
			}
			
			$MupList[]	= array(
				'nomer'		=> $nomer,
				'id'		=> $row['id'],
				'nazvanie'	=> $nazvanie,
				'etap'		=> $row['etap'],
				'ship'		=> $row['ship'],
				'tm'		=> $row['cena_tm'],
				'am'		=> $row['cena_am'],
				'ssilka'	=> $silka,
			);
		}
		
		$this->tplObj->assign_vars(array(
			'muplist'	=> $MupList,
		));
			
		$this->display('page.marktup.tpl');
	}
}
?>